package com.tanhua.server.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.common.pojo.User;
import com.tanhua.server.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

/**
 * @program: my-tanhua
 * @description: 通过sso接口查询
 * @author: xuejingjing
 * @create: 2021-09-21 13:02
 **/
@Service
@Slf4j
public class UserService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${tanhua,sso.url}")
    private String ssoUrl;

    private static final  ObjectMapper  MAPPER = new ObjectMapper();

    /**
     * 通过sso的rest接口查询
     * @param token
     * @return
     */
    public User queryUserByToken(String token) {

        try {
            String url = ssoUrl +"/user/"+ token;
            String data = restTemplate.getForObject(url,String.class);
            return  MAPPER.readValue(data, User.class);
        } catch (IOException e) {
            log.error("校验token出错, token = "+ token,e);

        }
        return null;
    }

}